package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.client.GoodsClient;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.domain.TCarts;
import com.woniu.car.dto.DeletCartsDto;
import com.woniu.car.dto.UpdateCartsDto;
import com.woniu.car.exception.ParamException;
import com.woniu.car.mapper.TCartsMapper;
import com.woniu.car.parameter.AddCartsParam;
import com.woniu.car.parameter.DeleteCartsParam;
import com.woniu.car.parameter.UpdateParam;
import com.woniu.car.service.TCartsService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author zhoubinbin
 * @since 2021-06-21
 */
@Service
public class TCartsServiceImpl extends ServiceImpl<TCartsMapper, TCarts> implements TCartsService {
    @Resource
    private TCartsMapper cartsMapper;
    @Resource
    private GoodsClient goodsCilent;

    //查询购物车
    @Override
    public ResponseEntity selectCarts(Integer userId) {
        QueryWrapper<TCarts> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<TCarts> tCarts = cartsMapper.selectList(wrapper);
        if (tCarts.size()==0){
            return new ResponseEntity(200,"购物车中未添加任何信息",null);
        }else{
            return new ResponseEntity(200,"查询成功",tCarts);
        }
    }
    //删除购物车
    @Override
    public ResponseEntity deleteCarts(DeleteCartsParam deleteCartsParam , Integer userId) throws ParamException {
        List<Integer> cartsId = deleteCartsParam.getCartsId();
        int[] h = new int[cartsId.size()];
        TCarts[] tCarts = new TCarts[cartsId.size()];
        DeletCartsDto[] mm = new DeletCartsDto[cartsId.size()];
        List<DeletCartsDto> catss = new ArrayList<>();

        for (int i=0;i<cartsId.size();i++){
            tCarts[i] = cartsMapper.selectById(cartsId.get(i));
            if(tCarts[i]==null||tCarts[i].getUserId()!=userId){
               throw new ParamException("请传入该用户正确的购物车id");
            }
            DeletCartsDto deletCartsDto = new DeletCartsDto();
            BeanUtils.copyProperties(tCarts[i],deletCartsDto);
            mm[i] = deletCartsDto;
            catss.add(mm[i]);
            h[i] = cartsMapper.deleteById(cartsId.get(i));
            if (h[i]==0){
                return new ResponseEntity().setMsg("删除失败");
            }
        }
        return new ResponseEntity(200,"删除成功",catss);
    }
    //修改购物车
    @Override
    public ResponseEntity updateCarts(UpdateParam updateParam) {
        List<Integer> cartsId = updateParam.getCartsId();
        List<Integer> quantity = updateParam.getQuantity();
        UpdateCartsDto[] cart = new UpdateCartsDto[cartsId.size()];
        for (int i=0;i<cartsId.size();i++){
            UpdateWrapper<TCarts> wrapper = new UpdateWrapper<>();
            wrapper.eq("carts_id",cartsId.get(i));
            TCarts tCarts = new TCarts();
            tCarts.setQuantity(quantity.get(i));
            tCarts.setAmout(updateParam.getAmout().get(i));
            int i1 = cartsMapper.update(tCarts, wrapper);
            TCarts tCarts1 = cartsMapper.selectById(cartsId.get(i));
            UpdateCartsDto cartsDto = new UpdateCartsDto();
            BeanUtils.copyProperties(tCarts1,cartsDto);
            if (i1>0||tCarts1!=null){
                cart[i] = cartsDto;
                return new ResponseEntity(200,"商品数量修改成功",cart);
            }
        }
        return new ResponseEntity(501,"商品数量修改失败,请输入正确的修改信息",null);
    }
    @Override
    public ResponseEntity addCarts(AddCartsParam addCartsParam, Integer userId) throws ParamException {
        if (addCartsParam.getQuantity()<=0){
            throw new ParamException("请传入正确的商品数量");
        }
        ResponseEntity goods = goodsCilent.getGoods(addCartsParam.getPioId());
        if (goods!=null){
            Object object = goods.getObject();
            if (object == null) {
                throw new ParamException("未查询到该商品的信息");
            }

            JSONArray jsonArray = JSONArray.fromObject(object);
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            String image = jsonObject.get("image").toString();
            String goodsName = jsonObject.get("goodsName").toString();
            Object goodsPrice = jsonObject.get("goodsPrice");
            double price = Double.parseDouble(goodsPrice.toString());
            TCarts tCarts = new TCarts();
            tCarts.setPioName(goodsName);
            tCarts.setPioPhoto(image);
            tCarts.setPioPrice(price);
            tCarts.setAmout(price*addCartsParam.getQuantity());
            tCarts.setUserId(userId);
            tCarts.setPioId(addCartsParam.getPioId());
            tCarts.setQuantity(addCartsParam.getQuantity());
            int insert = cartsMapper.insert(tCarts);
            if (insert>0){
                return new ResponseEntity(200,"购物车添加成功",null);
            }
        }else {
            throw new ParamException("未获取到该商品的信息");
        }
        return new ResponseEntity(400,"添加购物车失败",null);
    }
}
